package com.example.administrator.exam;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private Button bu_deng;
    private TextView et_yong;
    private TextView et_mi;
    private RequestQueue mRequestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_yong = (TextView) findViewById(R.id.et_yong);
        et_mi = (TextView) findViewById(R.id.et_mi);
        bu_deng = (Button) findViewById(R.id.bu_deng);
        jiexi();
    }
    public void jiexi(){
        bu_deng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRequestQueue =  Volley.newRequestQueue(MainActivity.this);
                String yonghu = et_yong.getText().toString().trim();
                String mima = et_mi.getText().toString().trim();
                String path = "http://api.gzkbapp.com/postLogin.action?userName="+yonghu+"&userPswd="+mima+"&vrfCd=1&cltId=1";
                StringRequest stringRequest = new StringRequest(path, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String user_id = jsonObject.optString("user_id");
                            String ssn_id = jsonObject.optString("ssn_id");
                            String login_date = jsonObject.optString("login_date");
                            String rtn_cd= jsonObject.optString("rtn_cd");
                            String rtn_msg = jsonObject.optString("rtn_msg");
                            Toast.makeText(getApplicationContext(),rtn_msg,Toast.LENGTH_LONG).show();
                            if (rtn_msg.equals("登录成功！")){
                                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                                intent.putExtra("user_id",user_id);
                                intent.putExtra("ssn_id",ssn_id);
                                startActivity(intent);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,"数据异常",Toast.LENGTH_LONG).show();
                    }
                });
                mRequestQueue.add(stringRequest);

            }


        });
    }
}
